package com.olocat.cjx.nsims.controller

import com.olocat.cjx.nsims.dao.LoginCacheRepository
import com.olocat.cjx.nsims.pojo.personnel.User
import com.olocat.cjx.nsims.service.UserService
import com.olocat.cjx.nsims.util.Response
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RestController
import javax.annotation.Resource

@RestController
class UserController {

    @Resource
    lateinit var userService: UserService
    @Resource
    lateinit var loginCacheRepository: LoginCacheRepository
    val log: Logger = LoggerFactory.getLogger("UserController")

    @PostMapping("/api/user/info")
    fun info(@RequestHeader("Access-Token") token:String): Response {
        log.info("get user info")
        log.info("token $token")
        val user: User? = loginCacheRepository.getByUserToken(token)?.userId?.let { userService.getById(it) }
        return Response.builder(user)
    }
    @GetMapping("/api/user/get")
    fun getUser(id:String):Response{
        return Response.builder(userService.getById(id))
    }
}